package com.zhongshengnetwork.rightbe.view.controller;

import android.content.Context;
//import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.pj.core.utilities.ConvertUtility;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.bindings.CommonItemBinding;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.CircleTransform;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.DateUtil;
import com.zhongshengnetwork.rightbe.gsonmodel.DryCommentModel;

public class CommonItemViewController extends BindingViewController<CommonItemBinding, DryCommentModel, Integer, CommonItemViewController.EventDispatcher> {

    public CommonItemViewController(CommonItemBinding binding) {
        super(binding);
    }

    public CommonItemViewController(View view) {
        super(view);
    }

    public CommonItemViewController(Context context, int layout, ViewGroup parent) {
        super(context, layout, parent);
    }

    @Override
    protected void onBind(CommonItemBinding binding) {

    }

    @Override
    public void setModel(final DryCommentModel model, final Integer position, final EventDispatcher eventDispatcher) {

        CommonItemBinding binding = getBinding();
        Context context = binding.getRoot().getContext();
        ImageView header = binding.drycommentListviewItemHeader;
        ImageView privateIcon = binding.drycommentListviewPrivateIcon;
        TextView time = binding.drycommentListviewItemTime;
        TextView name = binding.drycommentListviewItemName;
        TextView content = binding.drycommentListviewItemContent;
        RelativeLayout comment_listview_item_layout = binding.commentListviewItemLayout;

        LinearLayout drycomment_listview_item_header_layout = binding.drycommentListviewItemHeaderLayout;
        LinearLayout item_gas_line = binding.itemGasLine;
        drycomment_listview_item_header_layout.setClickable(true);
        drycomment_listview_item_header_layout.setTag(position);
        drycomment_listview_item_header_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (eventDispatcher != null) {
                    eventDispatcher.onComment((Integer) v.getTag());
                }
            }
        });

        name.setClickable(true);
        name.setTag(position);
        name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (eventDispatcher != null) {
                    eventDispatcher.onComment((Integer) v.getTag());
                }
            }
        });

        View drycomment_listview_item_more = binding.drycommentListviewItemMore;
        drycomment_listview_item_more.setClickable(true);
        drycomment_listview_item_more.setTag(position);
        drycomment_listview_item_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (eventDispatcher != null) {
                    eventDispatcher.onMoreAction((Integer) v.getTag());
                }
            }
        });
        RelativeLayout drycomment_listview_item_reply = binding.drycommentListviewItemReply;
        drycomment_listview_item_reply.setClickable(true);
        drycomment_listview_item_reply.setTag(position);
        drycomment_listview_item_reply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (eventDispatcher != null) {
                    eventDispatcher.onReplay((Integer) v.getTag());
                }
            }
        });
        TextView reply_text = binding.replyText;
        int replyCount = ConvertUtility.parseInt(model.getCcount());
        if (replyCount > 999){
            reply_text.setText((float)replyCount/1000.0f + "k回复");
        }else {
            if (replyCount > 0) {
                reply_text.setText(replyCount + "回复");
            } else {
                reply_text.setText("回复");
            }
        }

        View like_layout = binding.langLike;
        like_layout.setVisibility(View.VISIBLE);
        like_layout.setTag(position);
        like_layout.setFocusable(false);
        like_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (eventDispatcher != null) {
                    eventDispatcher.onActionLike((Integer) v.getTag());
                }
            }
        });
        ImageView like_icon = binding.langLikeImg;
        TextView like_count = binding.langLikeText;
        if (model.getIslike().equals("1")){
            like_icon.setImageResource(R.drawable.workgroup_img_like_sel);
            like_count.setTextColor(ContextCompat.getColor(context,R.color.lightGreenColor));
        }else {
            like_icon.setImageResource(R.drawable.workgroup_img_like);
            like_count.setTextColor(AppThemeUtils.getInstance().getSmallTextColor());
        }
        if (ConvertUtility.parseInt(model.getLcount()) < 1) {
            like_count.setText("笔芯");
        } else {
            like_count.setText(model.getLcount());
        }


        View collect_layout = binding.langCollect;
        collect_layout.setVisibility(View.VISIBLE);
        collect_layout.setTag(position);
        collect_layout.setFocusable(false);
        collect_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (eventDispatcher != null) {
                    eventDispatcher.onActionCollect((Integer) v.getTag());
                }
            }
        });
        ImageView collect_icon = binding.langCollectImg;
        TextView collect_count = binding.langCollectText;
        if (model.getIscollect().equals("1")){
            collect_icon.setImageResource(R.drawable.collection_sel);
            collect_count.setTextColor(ContextCompat.getColor(context,R.color.lightGreenColor));
        }else {
            collect_icon.setImageResource(R.drawable.collection);
            collect_count.setTextColor(AppThemeUtils.getInstance().getSmallTextColor());
        }
        collect_count.setText(ConvertUtility.parseInt(model.getCollectcount()) < 1 ? "收藏" : model.getCollectcount());

        Glide.with(context).load(model.getHeader()).transform(new CircleTransform(context)).dontAnimate().into(header);
        if (!CommonUtils.isEmpty(model.getPrivateIcon())){
            privateIcon.setVisibility(View.VISIBLE);
            Glide.with(context).load(model.getPrivateIcon()).transform(new CircleTransform(context)).dontAnimate().into(privateIcon);
        }else {
            privateIcon.setVisibility(View.INVISIBLE);
        }

        String lcount = CommonUtils.getDisplayCount(Integer.valueOf(model.getLcount()));
        String s1 = DateUtil.getChatDate(Long.valueOf(model.getTime()))+"";

        /*
        if (s1.contains("刚刚") || s1.contains("分钟") || s1.contains("小时")){
            String s2 = "";
            if (Integer.valueOf(model.getLcount()) > 0){
                s2 = "  ·  "+lcount+"笔芯";
            }
            time.setText(s1+s2);
        }else {
            time.setText(lcount+"笔芯");
        }
        */
        time.setText(s1);

        time.setTag(position);
        time.setFocusable(false);


        name.setText(model.getNickname());
        content.setTag(position);
        content.setText(model.getContent());
        content.setFocusable(false);

        comment_listview_item_layout.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
        item_gas_line.setBackgroundColor(AppThemeUtils.getInstance().getBackgroundColor());
        name.setTextColor(AppThemeUtils.getInstance().getUserNameColorOfWeiJu());
        content.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
        drycomment_listview_item_reply.setBackgroundResource(AppThemeUtils.getInstance().getCommentButtonBackground());
        time.setTextColor(AppThemeUtils.getInstance().getSmallTextColor());
    }

    public static interface EventDispatcher {
        public void onComment(Integer index);
        public void onMoreAction(Integer index);
        public void onReplay(Integer index);
        public void onActionLike(Integer index);
        public void onActionCollect(Integer index);
    }
}
